﻿/*
 * User.cs
 * 
 * Purpose:
 * Represents a user of the application. Provides a data structure for managing users and serializing user data for persistence.
 * 
 * Revision Log:
 * 08-SEP-12, C. Bush, First Draft.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Air_Trafic_Control.Domain.Common;
using Air_Trafic_Control.Domain.Entity;

namespace Air_Trafic_Control.Domain.Entity
{
    public class User
    {
        private string name = "";
        private int proficiency = 1;
        private List<Preference> preferences = new List<Preference>();

        /// <summary>
        /// Parameterless constructor required for serialization.
        /// </summary>
        public User()
        {
        }

        /// <summary>
        /// Create a new user with the name defined.
        /// </summary>
        /// <param name="name">The new user's name. This must be unique to the application.</param>
        public User(string name)
        {
            this.name = name;
        }

        /// <summary>
        /// Accessor and mutator for the User Name.
        /// </summary>
        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }

        /// <summary>
        /// Accessor and mutator for the Proficiency level. This also ensures that the level set does not violate the maximum
        /// and minimum proficiency levels set.
        /// </summary>
        public int Proficiency
        {
            get
            {
                return this.proficiency;
            }
            set
            {
                if (value < Constants.USER_MIN_PROFICIENCY)
                {
                    this.proficiency = Constants.USER_MIN_PROFICIENCY;
                }
                else if (value > Constants.USER_MAX_PROFICIENCY)
                {
                    this.proficiency = Constants.USER_MAX_PROFICIENCY;
                }
                else
                {
                    this.proficiency = value;
                }
            }
        }

        /// <summary>
        /// Returns the preference identified by the name provided.
        /// </summary>
        /// <param name="preferenceName">The name of the preference.</param>
        /// <returns>If a matching preference is found, that Preference object is returned. If no match is found, null is returned.</returns>
        public Preference GetPreference(string preferenceName)
        {
            foreach (Preference p in this.preferences)
            {
                if (p.Name.Equals(preferenceName))
                {
                    return p;
                }
            }

            return null;
        }

        /// <summary>
        /// Removes a preference.
        /// </summary>
        /// <param name="preferenceName">The name of the preference to remove.</param>
        /// <returns>If a matching preference is removed, true. If no match is found and removed, false.</returns>
        public bool RemovePreference(string preferenceName)
        {
            Preference preferenceToRemove = null;

            foreach (Preference p in this.preferences)
            {
                if (p.Name.Equals(preferenceName))
                {
                    preferenceToRemove = p;
                    break;
                }
            }

            if (preferenceToRemove != null)
            {
                this.preferences.Remove(preferenceToRemove);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Adds a preference.
        /// </summary>
        /// <param name="newPreference">The new Preference object to be added.</param>
        /// <returns>True if there did not already exist a Preference with the name of the new Preference and the new one was successfully added.</returns>
        public bool AddPreference(Preference newPreference)
        {
            foreach (Preference p in this.preferences)
            {
                if (p.Name.Equals(newPreference.Name))
                {
                    return false;
                }
            }
            this.preferences.Add(newPreference);

            return true;
        }

        /// <summary>
        /// Override the C# ToString method so we can add this directly to a ComboBox's Items collection
        /// </summary>
        /// <returns>The Name of the User.</returns>
        public override string ToString()
        {
            return this.name;
        }
    }
}
